package action;

import bean.Student;
import com.opensymphony.xwork2.ActionSupport;

import dao.StudentDAO;
import dao.StudentDAOImpl;
import tool.HibernateSessionFactory;

import java.util.List;
import java.util.Objects;

public class StudentAction extends ActionSupport {
    private static final long serialVersionUID = -3766131226463625778L;
    private StudentDAO studentDAO = new StudentDAOImpl(HibernateSessionFactory.getSession());
    private Student student;
    private List<Student> students;

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }

    public List<Student> getStudents() {
        return students;
    }

    @Override
    public String execute() throws Exception {
        students = studentDAO.queryAll();
        return super.execute();
    }

    public String add() {
        return studentDAO.add(student) ? "add" : null;
    }

    public String edit() {
        return Objects.nonNull(student = studentDAO.query(student.getStudentId())) ? "edit" : null;
    }

    public String modify() {
        return studentDAO.modify(student) ? "modify" : null;
    }

    public String remove() {
        return studentDAO.remove(student.getStudentId()) ? "remove" : null;
    }
}
